package com.kyrie.lib_sys.zTest.netflow;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * service层
 *
 * @author heefM
 * @date 2025-09-10
 */
@Service
public class FlowServiceImpl implements FlowService{

    @Autowired
    private FlowMapper flowMapper;

    @Override
    public int importFromExcel(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new RuntimeException("文件不能为空");
        }
        int count = 0;
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
            String line;
            // 跳过标题行
            reader.readLine();
            
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm");
            
            while ((line = reader.readLine()) != null) {
                String[] values = line.split(",");
                
                FlowRecord record = new FlowRecord();
                record.setInBytes(parseLong(values[0]));
                record.setInPkts(parseInt(values[1]));
                record.setOutBytes(parseLong(values[2]));
                record.setOutPkts(parseInt(values[3]));
                record.setIpSrcAddr(values[4]);
                record.setIpDstAddr(values[5]);
                record.setIpProtocol(parseInt(values[6]));
                record.setL4SrcPort(parseInt(values[7]));
                record.setL4DstPort(parseInt(values[8]));
                record.setTos(parseInt(values[9]));
                record.setDstTos(parseInt(values[10]));
                record.setTcpFlags(parseInt(values[11]));
                record.setStartTime(parseDateTime(values[12], formatter));
                record.setEndTime(parseDateTime(values[13], formatter));
                record.setSrcRegion(values[14]);
                record.setDstRegion(values[15]);
                record.setSrcGroup(values[16]);
                record.setDstGroup(values[17]);
                record.setArea(values[18]);
                record.setFlowSampI(parseInt(values[19]));
                record.setUserScope(parseInt(values[20]));
                
                flowMapper.insert(record);
                count++;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导入数据失败: " + e.getMessage());
        }
        return count;
    }

    @Override
    public int getRecordCount() {
        return 0;
    }
    
    private Long parseLong(String value) {
        try {
            return Long.parseLong(value);
        } catch (NumberFormatException e) {
            return 0L;
        }
    }
    
    private Integer parseInt(String value) {
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return 0;
        }
    }
    
    private LocalDateTime parseDateTime(String value, DateTimeFormatter formatter) {
        try {
            return LocalDateTime.parse(value, formatter);
        } catch (Exception e) {
            return null;
        }
    }
}